package com.enqualcomm.kids.view.fencing;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.enqualcomm.kids.R;


import common.utils.DensityUtil;

/**
 * Created by yinzhiqun on 2015/12/24.
 * 覆盖在地图上，显示宽高/半径，名字
 */
public class FencingView extends RelativeLayout implements IFencingView {

    private TextView heightTv;
    private TextView diameterTv;
    private TextView widthTv;
    private TextView fencingNameTv;
    private Context context;
    boolean isCircle;
    boolean hasInit;
    int width;
    int height;
    int diameter;

    /**
     * 每米scale个像素,得有一个默认值，否则最开始的时候会没有值
     */
    float scale= 0.3f;

    public FencingView(Context context) {
        super(context);
        this.context = context;
    }

    public FencingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public FencingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
    }

    private void init() {
        setClipChildren(false);
        widthTv = createTextView();
        LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        addView(widthTv, layoutParams);

        heightTv = createTextView();
        layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
        addView(heightTv, layoutParams);

        diameterTv = createTextView();
        layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        addView(diameterTv, layoutParams);

        fencingNameTv = createTextView();

        fencingNameTv.setTextSize(16);
        fencingNameTv.setText(context.getString(R.string.fence_press_edit));
        fencingNameTv.setTextColor(Color.rgb(0x00,0x1a,0xfd));

        fencingNameTv.setSingleLine(true);//避免换行
        layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        layoutParams.topMargin = DensityUtil.dip2px(getContext(),16);
        addView(fencingNameTv, layoutParams);
    }

    private TextView createTextView(){
        TextView textView = new TextView(getContext());
        textView.setTextColor(Color.rgb(0xff, 0x00, 0x00));
        textView.setVisibility(View.INVISIBLE);
        return textView;
    }

    @Override
    public void update(int width, int height) {
        width = width>2000?2000:width;
        width = width<200?200:width;
        height = height>2000?2000:height;
        height = height<200?200:height;

        this.width = width;
        this.height = height;
//        if (scale==0){
//            return;
//        }
        if (!isCircle){
            if (!hasInit){
                hasInit = true;
                init();
            }

            ViewGroup.LayoutParams layoutParams = getLayoutParams();
            layoutParams.width = (int) (width*scale);
            layoutParams.height = (int) (height *scale);
            setLayoutParams(layoutParams);

            widthTv.setText(width+"m");
            heightTv.setText(height + "m");
        }
    }

    @Override
    public void update(int diameter) {
        diameter = diameter>2000?2000:diameter;
        diameter = diameter<200?200:diameter;

        this.diameter =diameter;
//        if (scale==0){
//            return;
//        }
        if (isCircle){
            if (!hasInit){
                hasInit = true;
                init();
            }

            ViewGroup.LayoutParams layoutParams = getLayoutParams();
            layoutParams.width = (int) (diameter*scale);
            layoutParams.height = (int) (diameter *scale);
            setLayoutParams(layoutParams);

            diameterTv.setText(diameter + "m");
        }
    }

    @Override
    public void setFencingName(String fencingName) {
//        if (!hasInit){
//            hasInit = true;
//            init();
//        }
//        fencingNameTv.setText(String.format("长按拖动设置%s的范围", fencingName));
    }

    @Override
    public void showTextviews() {
        if (isCircle){
            diameterTv.setVisibility(View.VISIBLE);
        }else {
            widthTv.setVisibility(View.VISIBLE);
            heightTv.setVisibility(View.VISIBLE);
        }
        fencingNameTv.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideTextViews() {
        fencingNameTv.setVisibility(View.INVISIBLE);
        diameterTv.setVisibility(View.INVISIBLE);
        widthTv.setVisibility(View.INVISIBLE);
        heightTv.setVisibility(View.INVISIBLE);
    }

    @Override
    public void setMeterPx(float px) {
        if (px!=scale){
            this.scale = px;
            if (isCircle){
                update(diameter);
            }else {
                update(width,height);
            }
        }
    }

    @Override
    public float calculateSize() {
        if (isCircle){
            return diameter;
        }else{
            return width>height?width:height;
        }
    }

    @Override
    public void setIsCircle(boolean isCricle) {
        this.isCircle = isCricle;
        if (!hasInit){
            hasInit = true;
            init();
        }

        if (isCricle){
            widthTv.setVisibility(View.INVISIBLE);
            heightTv.setVisibility(View.INVISIBLE);
            setBackgroundResource(R.drawable.bg_circle_fencing);
        }else{
            diameterTv.setVisibility(View.INVISIBLE);
            setBackgroundResource(R.drawable.bg_rect_fencing);
        }
    }

    @Override
    public boolean isCircle() {
        return isCircle;
    }

    @Override
    public int getFencingDiameter() {
        return diameter;
    }

    @Override
    public Rect getFencingRect() {
//        Rect rect = new Rect();
//        getGlobalVisibleRect(rect);
//        //TODO 需要减去标题栏状态栏的高度
        int top = getTop();
        int left = getLeft();
        int right = getRight();
        int bottom = getBottom();

        return new Rect(left,top,right,bottom);
    }


    /**
     * x轴缩放
     */
    public void zoomX(float offsetX) {
        float x = offsetX / scale;

        if (isCircle){
            update((int) (diameter + x * 1.42));
        }else{
            update((int) (width + x * 2), height);
        }
    }

    /**
     * y轴缩放
     */
    public void zoomY(float offsetY) {
        float y = offsetY / scale;

        if (isCircle){
            update((int) (diameter+ y*1.42));
        }else{
            update(width,(int) (height + y*2));
        }
    }

    /**
     * 进入/退出编辑模式
     * @param b
     */
    public void setEditMode(boolean b){
        if (b){// 修改背景颜色
            if (isCircle){
                setBackgroundResource(R.drawable.bg_circle_fencing_selected);
            }else{
                setBackgroundResource(R.drawable.bg_rect_fencing_selected);
            }
        }else{
            if (isCircle){//恢复正常颜色
                setBackgroundResource(R.drawable.bg_circle_fencing);
            }else{
                setBackgroundResource(R.drawable.bg_rect_fencing);
            }
        }
    }

}
